// miceshot.cpp : Implementation of DLL Exports.

#include "stdafx.h"

#include <list>

#include "utility.h"
#include "pcselfy.h"
#include "dllmain.h"

typedef	ATL::CComObjectCached< ATL::CComClassFactory >	CFACTORY;
class CmiceshotModule : public ATL::CAtlDllModuleT< CmiceshotModule >
{
//	DECLARE_LIBID(LIBID_miceshotLib)
//	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MICESHOT, "{664273CD-39A5-4EA1-925D-1686545B70E3}")
};

CmiceshotModule _AtlModule;
//f ATL::CComCreator2< ATL::CComCreator<ATL::CComObject< CPCSelfy > >, ATL::CComCreator< ATL::CComAggObject<CPCSelfy> > > CComPCSelfy;
//extern CAtlComModule _AtlComModule;
CWin32Heap _Win32Heap(GetProcessHeap());

// DLL Entry Point
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
//	hInstance;
	return _AtlModule.DllMain(dwReason, lpReserved); 
}

// Used to determine whether the DLL can be unloaded by OLE.
STDAPI DllCanUnloadNow(void)
{
	return (_AtlModule.GetLockCount()==0) ? S_OK : S_FALSE;
	return _AtlModule.DllCanUnloadNow();
}

// Returns a class factory to create an object of the requested type.
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
 HRESULT hr = ATL::CComCreator<CFACTORY>::CreateInstance(NotAtlCreInstance, riid, ppv);
//	if (SUCCEEDED(hr) && (* ppv)) ((IUnknown *)(* ppv))->AddRef();
	return hr;
	return ATL::AtlComModuleGetClassObject(& ATL::_AtlComModule, rclsid, riid, ppv);
}

// DllRegisterServer - Adds entries to the system registry.
STDAPI DllRegisterServer(void)
{
	// registers object, typelib and all interfaces in typelib
//	_AtlModule.DllRegisterServer();
 HRESULT hr = _AtlModule.UpdateRegistryFromResource(IDR_PCSELFY, TRUE);
//	if (SUCCEEDED(hr)) hr = ATL::_AtlComModule.RegisterTypeLib();
	return hr;
}

// DllUnregisterServer - Removes entries from the system registry.
STDAPI DllUnregisterServer(void)
{
//	HRESULT hr = ATL::_AtlComModule.UnRegisterTypeLib();
 HRESULT hr = _AtlModule.UpdateRegistryFromResource(IDR_PCSELFY, FALSE);
	return hr;
}

// DllInstall - Adds/Removes entries to the system registry per user per machine.
STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
{
	static const wchar_t szUserSwitch[] = L"user";
	if (pszCmdLine != NULL)
	if (!(::_wcsnicmp(pszCmdLine, szUserSwitch, _countof(szUserSwitch))))
			ATL::AtlSetPerUserRegistration(true);

	HRESULT hr = E_FAIL;
	if (bInstall)
	{	
		hr = DllRegisterServer();
		if (FAILED(hr)) DllUnregisterServer();
	}
		else 
			hr = DllUnregisterServer();

	return hr;
}
